/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastroModelo.java
 *
 * Created on 05/04/2011, 01:24:05
 */
package Tela;

import Classe.BD;
import Classe.Marca;
import Classe.Modelo;
import Classe.Validacao;
import java.awt.Component;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author gladson
 */
public class CadastroModelo extends Validacao {

    private Component frameI;

    /** Creates new form CadastroModelo */
    public CadastroModelo() {
        initComponents();
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        iniciar();
    }
    CadastroVeiculo vc;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jpnCadastroModelo = new javax.swing.JPanel();
        btSalvarModelo = new javax.swing.JButton();
        btLimparModelo = new javax.swing.JButton();
        btSairModelo = new javax.swing.JButton();
        lblMarcaModelo = new javax.swing.JLabel();
        cbMarcaModelo = new javax.swing.JComboBox();
        lblModeloModelo = new javax.swing.JLabel();
        sTxtModeloModelo = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jpnCadastroModelo.setBackground(new java.awt.Color(255, 255, 255));
        jpnCadastroModelo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Cadastro Modelo ", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("PMingLiU", 0, 36))); // NOI18N

        btSalvarModelo.setFont(new java.awt.Font("Arial", 0, 12));
        btSalvarModelo.setText("Salvar");
        btSalvarModelo.setToolTipText("Salvar");
        btSalvarModelo.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btSalvarModelo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarModeloActionPerformed(evt);
            }
        });

        btLimparModelo.setFont(new java.awt.Font("Arial", 0, 12));
        btLimparModelo.setText("Limpar");
        btLimparModelo.setToolTipText("Limpar");
        btLimparModelo.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btLimparModelo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLimparModeloActionPerformed(evt);
            }
        });

        btSairModelo.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        btSairModelo.setText("Sair");
        btSairModelo.setToolTipText("Sair");
        btSairModelo.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btSairModelo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairModeloActionPerformed(evt);
            }
        });

        lblMarcaModelo.setFont(new java.awt.Font("Arial", 1, 11));
        lblMarcaModelo.setText("Marca");

        cbMarcaModelo.setModel(new javax.swing.DefaultComboBoxModel(new String[] {null}));
        cbMarcaModelo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        lblModeloModelo.setFont(new java.awt.Font("Arial", 1, 11));
        lblModeloModelo.setText("Modelo");

        sTxtModeloModelo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        javax.swing.GroupLayout jpnCadastroModeloLayout = new javax.swing.GroupLayout(jpnCadastroModelo);
        jpnCadastroModelo.setLayout(jpnCadastroModeloLayout);
        jpnCadastroModeloLayout.setHorizontalGroup(
            jpnCadastroModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnCadastroModeloLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnCadastroModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnCadastroModeloLayout.createSequentialGroup()
                        .addComponent(btSalvarModelo)
                        .addGap(18, 18, 18)
                        .addComponent(btLimparModelo)
                        .addGap(18, 18, 18)
                        .addComponent(btSairModelo))
                    .addComponent(sTxtModeloModelo, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
                    .addComponent(cbMarcaModelo, 0, 469, Short.MAX_VALUE)
                    .addComponent(lblMarcaModelo)
                    .addComponent(lblModeloModelo))
                .addContainerGap())
        );

        jpnCadastroModeloLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btLimparModelo, btSairModelo, btSalvarModelo});

        jpnCadastroModeloLayout.setVerticalGroup(
            jpnCadastroModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpnCadastroModeloLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMarcaModelo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbMarcaModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblModeloModelo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sTxtModeloModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addGroup(jpnCadastroModeloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSalvarModelo)
                    .addComponent(btLimparModelo)
                    .addComponent(btSairModelo))
                .addContainerGap())
        );

        jpnCadastroModeloLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btLimparModelo, btSairModelo, btSalvarModelo});

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpnCadastroModelo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpnCadastroModelo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-529)/2, (screenSize.height-267)/2, 529, 267);
    }// </editor-fold>//GEN-END:initComponents

    private void btSairModeloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairModeloActionPerformed
        this.dispose();
        // TODO add your handling code here:
}//GEN-LAST:event_btSairModeloActionPerformed

    private void btSalvarModeloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarModeloActionPerformed
        if (validar()) {//confirmando se a validação dos campos pelo metodo validação

            try {
                Modelo modelo = new Modelo();

                modelo.setModelo(sTxtModeloModelo.getText().trim().toUpperCase());

                Marca itemMarca = (Marca) cbMarcaModelo.getSelectedItem();

                String sql = "insert into Modelo (id_modelo,modelo,id_marca) values (sq_modelo.nextval,?,?)";

                Object[] parametros = {modelo.getModelo(), itemMarca.getIdMarca()};
                Object[] tipos = {String.class, Integer.class};


                BD banco = new BD();
                banco.executar(sql, parametros, tipos);
                JOptionPane.showMessageDialog(frameI, "Cadastro salvo com Sucesso");
                iniciar();
                sTxtModeloModelo.setText("");
                this.iniciar();

            } catch (Exception ex) {
                ex.printStackTrace();

            }
        }
    }//GEN-LAST:event_btSalvarModeloActionPerformed

    private void btLimparModeloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLimparModeloActionPerformed
        iniciar();
        sTxtModeloModelo.setText("");

    }//GEN-LAST:event_btLimparModeloActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadastroModelo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btLimparModelo;
    private javax.swing.JButton btSairModelo;
    private javax.swing.JButton btSalvarModelo;
    private javax.swing.JComboBox cbMarcaModelo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jpnCadastroModelo;
    private javax.swing.JLabel lblMarcaModelo;
    private javax.swing.JLabel lblModeloModelo;
    private javax.swing.JTextField sTxtModeloModelo;
    // End of variables declaration//GEN-END:variables

    private void iniciar() {
        try {
            String sql = "Select * from marca order by marca";
            BD bd = new BD();
            ResultSet r1 = bd.consultar(sql);
            cbMarcaModelo.removeAllItems();

            cbMarcaModelo.addItem(null);

            while (r1.next()) {
                Marca marca = new Marca();
                int id_marca = r1.getInt("id_marca");
                String nome_marca = r1.getString("marca");

                marca.setIdMarca(id_marca);
                marca.setNomeMarca(nome_marca);

                cbMarcaModelo.addItem(marca);
            }



        } catch (ClassNotFoundException ex) {
        } catch (SQLException ex) {
        }
    }

    public void pegar(CadastroVeiculo cv) {
        this.vc = cv;
    }

    public boolean validar() {
        boolean Resultado = true;

        if (cbMarcaModelo.getSelectedItem() == null) {
            JOptionPane.showMessageDialog(rootPane, "Seleção do campo 'Marca' é Obrigatório!!");
            Resultado = false;
        } else if (validarModelo(sTxtModeloModelo.getText().trim()) == false) {
            Resultado = false;
        }

        return Resultado;
    }
}
